package com.biyao.es.utils;

import com.biyao.es.dto.Content;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * 数据爬取
 *
 * @author: hxs
 * @create: 2020/7/7
 */

public class HtmlParseUtil {

    public static void main(String[] args) throws Exception {
        new HtmlParseUtil().parseJD("java").forEach(System.out::println);
        new HtmlParseUtil().parseTM("java").forEach(System.out::println);
    }

    /**
     * 功能描述 京东数据获取
     *
     * @param keyWords 关键字
     * @return java.util.List<com.biyao.es.dto.Content>
     * @author hxs
     * @date 2020/7/7
     */
    public List<Content> parseJD(String keyWords) throws Exception {
        String url = "https://search.jd.com/Search?keyword=" + keyWords + "&enc=utf-8";
        Document document = Jsoup.parse(new URL(url), 30000);
        Element element = document.getElementById("J_goodsList");
        Elements elements = element.getElementsByTag("li");
        List<Content> goodList = new ArrayList<>();
        for (Element el : elements) {
            String img = el.getElementsByTag("img").eq(0).attr("src");
            String price = el.select("div.p-price > strong > i").eq(0).text();
            String title = el.getElementsByClass("p-name").eq(0).text();
            Content content = new Content().builder()
                    .title(title)
                    .img(img)
                    .price(price).build();
            goodList.add(content);
        }
        return goodList;
    }

    /**
     * 功能描述 天猫数据获取
     *
     * @param keyWords 关键字
     * @return java.util.List<com.biyao.es.dto.Content>
     * @author hxs
     * @date 2020/7/7
     */
    public List<Content> parseTM(String keyWords) throws Exception {
        String url = "https://list.tmall.com/search_product.htm?q=" + keyWords + "&type=p&cat=all";
        Document document = Jsoup.parse(new URL(url), 30000);
        Element element = document.getElementById("J_ItemList");
        Elements elements = element.getElementsByClass("product");
        List<Content> goodList = new ArrayList<>();
        for (Element el : elements) {
            String img = el.getElementsByTag("img").eq(0).attr("src");
            String price = el.getElementsByClass("productPrice").eq(0).text();
            String title = el.getElementsByClass("productTitle").eq(0).text();
            Content content = new Content().builder()
                    .title(title)
                    .img(img)
                    .price(price).build();
            goodList.add(content);
        }
        return goodList;
    }

} 